﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class Sys_mailother : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PM.Code.Web.controlbind.BindDropDownList(this.ddlSender, "select collegename from Dict_College", "collegename", "collegename");
            this.ddlSender.Items.Insert(0, new ListItem("请选择", "-1"));
            BindGridView();
        }
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindGridView();
    }
    protected void btnSeach_Click(object sender, ImageClickEventArgs e)
    {
        BindGridView();
    }
    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.ddlSender.Items.Clear();//清除所有
        string sqlstr="";
        if (this.ddlType.SelectedValue == "2")
        {
            this.ddlSender.Enabled = true;
            sqlstr = "select collegename from Dict_College";
            PM.Code.Web.controlbind.BindDropDownList(this.ddlSender, sqlstr, "collegename", "collegename");
        }
        else if (this.ddlType.SelectedValue == "3")
        {
            this.ddlSender.Enabled = true;
            sqlstr = "select tid,[name] from Sys_Account where usertype=3";
            PM.Code.Web.controlbind.BindDropDownList(this.ddlSender, sqlstr, "name", "tid");
        }
        else
            this.ddlSender.Enabled = false;
        this.ddlSender.Items.Insert(0,new ListItem("请选择","-1"));
        this.ddlSender.SelectedIndex = 0;
        BindGridView();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string msgid = e.CommandArgument.ToString().Trim();
        if (e.CommandName == "del")
        {
            PM.PMBasic.Mail Mail = new PM.PMBasic.Mail();
            Mail.Delete_Msg(msgid);
            BindGridView();
        }
    }
    protected void BindGridView()
    {
        try
        {
            PM.PMBasic.Mail Mail = new PM.PMBasic.Mail();
            StringBuilder sqlstr = new StringBuilder(" 1=1 ");
            getWhere(ref sqlstr);
            this.GridView1.DataSource=Mail.GetMail(sqlstr.ToString(), 15, this.AspNetPager1);
            this.GridView1.DataBind();
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
    }
    protected void getWhere(ref StringBuilder sqlstr)
    {
        if (this.ddlType.SelectedValue == "2")
        {
            sqlstr.Append(" and [type]=2");
            if (this.ddlSender.SelectedIndex != 0)
                sqlstr.Append(" and depart='").Append(this.ddlSender.SelectedValue.Trim()).Append("'");
        }
        else if (this.ddlType.SelectedValue == "3")
        {
            sqlstr.Append(" and [type]=3");
            if (this.ddlSender.SelectedIndex != 0)
                sqlstr.Append(" and sender='").Append(this.ddlSender.SelectedValue.Trim()).Append("'");
        }
        else if (this.ddlType.SelectedValue == "4")
            sqlstr.Append(" and [type]=4");
        if (this.txtStart.Value.Trim() != string.Empty)
            sqlstr.Append(" and createdate>'").Append(this.txtStart.Value.Trim()).Append("'");
        if (this.txtEnd.Value.Trim() != string.Empty)
            sqlstr.Append(" and createdate<'").Append(this.txtEnd.Value.Trim()).Append("'");
    }
    public string strReceive()
    {
        if (this.ddlType.SelectedValue == "2")
            return "学院用户";
        else if (this.ddlType.SelectedValue == "3")
            return "学院管理员";
        else
            return "所有用户";
    }
}
